package com.train.course.controller;

import com.train.common.annotation.Anonymous;
import com.train.common.core.controller.BaseController;
import com.train.common.core.domain.AjaxResult;
import com.train.common.core.page.TableDataInfo;
import com.train.course.domain.VideoTag;
import com.train.course.dto.CourseTagDTO;
import com.train.course.service.IVideoTagService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 视频标签 控制器
 */
@RestController
@RequestMapping("/course/tag")
public class VideoTagController extends BaseController {
    
    @Autowired
    private IVideoTagService videoTagService;

    /**
     * 查询视频标签列表
     */
    @GetMapping("/list")
    public TableDataInfo list(VideoTag videoTag) {
        startPage();
        List<VideoTag> list = videoTagService.selectVideoTagList(videoTag);
        return getDataTable(list);
    }

    /**
     * 获取视频标签详细信息
     */
    @GetMapping(value = "/{tagId}")
    public AjaxResult getInfo(@PathVariable("tagId") Long tagId) {
        return success(videoTagService.selectVideoTagById(tagId));
    }

    /**
     * 新增视频标签
     */
    @PostMapping
    public AjaxResult add(@RequestBody VideoTag videoTag) {
        return toAjax(videoTagService.insertVideoTag(videoTag));
    }

    /**
     * 修改视频标签
     */
    @PutMapping
    public AjaxResult edit(@RequestBody VideoTag videoTag) {
        return toAjax(videoTagService.updateVideoTag(videoTag));
    }

    /**
     * 删除视频标签
     */
    @DeleteMapping("/{tagIds}")
    public AjaxResult remove(@PathVariable Long[] tagIds) {
        return toAjax(videoTagService.deleteVideoTagByIds(tagIds));
    }

    /**
     * 为课程添加标签
     */
    @Anonymous
    @PostMapping("/addCourseTags")
    public AjaxResult addCourseTags(@Validated @RequestBody CourseTagDTO courseTagDTO) {
        int result = videoTagService.addCourseTags(courseTagDTO);
        return result > 0 ? success("添加标签成功") : error("添加标签失败");
    }

    /**
     * 查询课程的标签列表
     */
    @Anonymous
    @GetMapping("/course/{courseId}")
    public AjaxResult getCourseTags(@PathVariable("courseId") Long courseId) {
        List<VideoTag> tags = videoTagService.selectTagsByVideoId(courseId, "1");
        return success(tags);
    }
}
